<template>
  <div class="container">
    <div class="title">  西电统一身份认证  </div>
    <el-form ref="ruleForm" :model="data" :rules="rules" status-icon label-width="70px">
      <el-form-item label="账号：" prop="username">
        <el-input v-model="data.username" type="text" autocomplete="off" />
      </el-form-item>
      <el-form-item label="密码：" prop="password">
        <el-input v-model="data.password" type="password" autocomplete="off" />
      </el-form-item>
      <el-button type="primary" class="button-login" :loading="loading" @click="submitForm()">登录</el-button>
    </el-form>
  </div>
</template>

<script>
import { login, check_login } from '@/utils/login'
import Cookie from 'js-cookie'
export default {
  name: 'Home',
  data() {
    return {
      data: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '账号不能为空', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '密码不能为空', trigger: 'blur' }
        ]
      },
      loading: false
    }
  },
  mounted() {
    check_login().then(res => {
      this.$router.push('/find')
    })
  },
  methods: {
    submitForm() {
      this.loading = true
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          login(this.data).then(res => {
            const data = JSON.parse(JSON.stringify(res.data))
            for (const key in data) {
              Cookie.set(key, data[key])
            }
            Cookie.set('EMAP_LANG', 'zh')
            Cookie.set('copy_WEU', data['_WEU'])
            Cookie.set('THEME', 'cherry')
            Cookie.set('amp.locale', 'undefined')
            this.loading = false
            this.$router.push('/find')
          }).catch(err => {
            this.loading = false
            this.$message({
              showClose: true,
              message: '账号或密码错误',
              type: 'warning'
            })
            console.log(err)
          })
        } else {
          return false
        }
      })
    }
  }

}
</script>

<style scoped  lang="scss">
  .container {

    width: 30%;
    min-width: 300px;
    margin: 50px auto;
    .title {
      margin-bottom: 50px;
      font-size: 22px;
      font-weight: bold;
    }
  }
  .button-login {
    // font-weight: bold;
    font-size: 20px;
    width: 50%;
    max-width: 100px;
  }
</style>

